package com.dm.saleassfront.controller;

import com.dm.saleassfront.common.*;
import com.dm.saleassfront.pojo.AssessExcel;
import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.apache.poi.hssf.usermodel.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping(value = "goSaleBack")
public class GoController {
    private static final Logger logger = LoggerFactory.getLogger(GoController.class);

//    private static String BACKURL = "http://127.0.0.1:8071";
    private static String BACKURL = PropertyUtil.getValue("backurl");
//    private static String BACKURL = "http://222.222.33.34:6443/saleassback";
//private static String BACKURL = "http://192.168.99.21:8080/saleassback";


    @Autowired
    private ImageToBase imageToBase;

    @Autowired
    private OkHttpUtils okHttpUtils;


    /*####################################################################################*/
    /*##############################   登陆管理    #######################################*/
    /*####################################################################################*/

    @RequestMapping(value="/login",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String,Object> goLogin(@RequestBody Map<String,Object> param, HttpServletRequest request){
        JSONObject jsonObject = JSONObject.fromObject(param);
        Map<String,String> headersParams = new HashMap<String,String>();
        headersParams.put("Content-Type","application/json;UTF-8");
        Map<String,Object> statusMap = okHttpUtils.sendPostJson(BACKURL+"/login",headersParams,jsonObject.toString(),new Object());
        JSONObject resultJson = JSONObject.fromObject(statusMap.get("data"));
        Map json = (Map) JSONObject.fromObject(resultJson);
        HttpSession session = request.getSession();
        if(json.size()>1){
            String username = json.get("username").toString();
            String store = json.get("store").toString();
            String userno = json.get("userno").toString();
            session.setAttribute("userno",userno);
            session.setAttribute("username",username);
            session.setAttribute("store",store);
            session.setMaxInactiveInterval(60*60*4);
        }
        return json;
    }

    /**
         * @Description 初始化界面 查看是否存有session 并获取所有门店
         * @Author zhangxue
         * @Date 9:22 2018/6/3
         * @Param
         * @retrun
         **/
    @RequestMapping(value="/initPage",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String initPage(HttpSession session){
        String username = session.getAttribute("username")!=null?session.getAttribute("username").toString():null;
        String Store = session.getAttribute("store")!=null?session.getAttribute("store").toString():null;
        String userno = session.getAttribute("userno")!=null?session.getAttribute("userno").toString():null;
        Map<String,Object> tMap = new HashMap<>();
        tMap.put("username",username);
        tMap.put("store",Store);
        tMap.put("userno",userno);
        JSONObject js = new JSONObject();
        if(username==null||Store==null){
            tMap.put("status","0");
            js = JSONObject.fromObject(tMap);
            return js.toString();
        }else{
            JSONObject jsonObject = JSONObject.fromObject(tMap);
            Map<String,String> headersParams = new HashMap<String,String>();
            headersParams.put("Content-Type","application/json;UTF-8");
            Map<String,Object> statusMap = okHttpUtils.sendPostJson(BACKURL+"/initPage",headersParams,jsonObject.toString(),new Object());
            statusMap.put("status","1");
            statusMap.put("store",Store);
            statusMap.put("username",username);
            js = JSONObject.fromObject(statusMap);
            return js.toString();
        }
    }

    //类别下拉
    @RequestMapping(value="/queryAllCategorySelect",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String queryAllCategorySelect(@RequestParam Map<String,Object> param){
        JSONObject jsonObject = JSONObject.fromObject(param);
        Map<String,String> headersParams = new HashMap<String,String>();
        headersParams.put("Content-Type","application/json;UTF-8");
        Map<String,Object> resultMap = okHttpUtils.sendPostJson(BACKURL+"/queryAllCategorySelect",headersParams,jsonObject.toString(),new Object());
        JSONObject js = new JSONObject();
        js = JSONObject.fromObject(resultMap);
        return js.toString();

    }

    //项目下拉
    @RequestMapping(value="/queryAllProjectSelect",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String queryAllProjectSelect(@RequestBody Map<String,Object> param){
        JSONObject jsonObject = JSONObject.fromObject(param);
        Map<String,String> headersParams = new HashMap<String,String>();
        headersParams.put("Content-Type","application/json;UTF-8");
        Map<String,Object> resultMap = okHttpUtils.sendPostJson(BACKURL+"/queryAllProjectSelect",headersParams,jsonObject.toString(),new Object());
        JSONObject js = new JSONObject();
        js = JSONObject.fromObject(resultMap);
        return js.toString();

    }

    //岗位标准下拉
    @RequestMapping(value="/queryAllStandardSelect",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String queryAllStandardSelect(@RequestParam Map<String,Object> param){
        JSONObject jsonObject = JSONObject.fromObject(param);
        Map<String,String> headersParams = new HashMap<String,String>();
        headersParams.put("Content-Type","application/json;UTF-8");
        Map<String,Object> resultMap = okHttpUtils.sendPostJson(BACKURL+"/queryAllStandardSelect",headersParams,jsonObject.toString(),new Object());
        JSONObject js = new JSONObject();
        js = JSONObject.fromObject(resultMap);
        return js.toString();

    }

    //查询所有销售部人员
    @RequestMapping(value="/queryAllSaleMan",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String queryAllSaleMan(@RequestParam Map<String,Object> param){
        JSONObject jsonObject = JSONObject.fromObject(param);
        Map<String,String> headersParams = new HashMap<String,String>();
        headersParams.put("Content-Type","application/json;UTF-8");
        Map<String,Object> resultMap = okHttpUtils.sendPostJson(BACKURL+"/queryAllSaleMan",headersParams,jsonObject.toString(),new Object());
        JSONObject js = new JSONObject();
        js = JSONObject.fromObject(resultMap);
        return js.toString();

    }

    //查询所有销售部人员下拉
    @RequestMapping(value="/queryAllSaleManSelect",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String queryAllSaleManSelect(@RequestBody Map<String,Object> param){
        JSONObject jsonObject = JSONObject.fromObject(param);
        Map<String,String> headersParams = new HashMap<String,String>();
        headersParams.put("Content-Type","application/json;UTF-8");
        Map<String,Object> resultMap = okHttpUtils.sendPostJson(BACKURL+"/queryAllSaleManSelect",headersParams,jsonObject.toString(),new Object());
        JSONObject js = new JSONObject();
        js = JSONObject.fromObject(resultMap);
        return js.toString();

    }




    //查询所有销售部岗位信息
    @RequestMapping(value="/queryAllSalePost",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String queryAllSalePost(@RequestParam Map<String,Object> param){
        JSONObject jsonObject = JSONObject.fromObject(param);
        Map<String,String> headersParams = new HashMap<String,String>();
        headersParams.put("Content-Type","application/json;UTF-8");
        Map<String,Object> resultMap = okHttpUtils.sendPostJson(BACKURL+"/queryAllSalePost",headersParams,jsonObject.toString(),new Object());
        JSONObject js = new JSONObject();
        js = JSONObject.fromObject(resultMap);
        return js.toString();

    }

    //查询所有销售部岗位下拉信息
    @RequestMapping(value="/queryAllSalePostSelect",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String queryAllSalePostSelect(@RequestBody Map<String,Object> param){
        JSONObject jsonObject = JSONObject.fromObject(param);
        Map<String,String> headersParams = new HashMap<String,String>();
        headersParams.put("Content-Type","application/json;UTF-8");
        Map<String,Object> resultMap = okHttpUtils.sendPostJson(BACKURL+"/queryAllSalePostSelect",headersParams,jsonObject.toString(),new Object());
        JSONObject js = new JSONObject();
        js = JSONObject.fromObject(resultMap);
        return js.toString();

    }


    //查询所有销售部门店
    @RequestMapping(value="/queryAllSaleStore",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String queryAllSaleStore(@RequestParam Map<String,Object> param){
        JSONObject jsonObject = JSONObject.fromObject(param);
        Map<String,String> headersParams = new HashMap<String,String>();
        headersParams.put("Content-Type","application/json;UTF-8");
        Map<String,Object> resultMap = okHttpUtils.sendPostJson(BACKURL+"/queryAllSaleStore",headersParams,jsonObject.toString(),new Object());
        JSONObject js = new JSONObject();
        js = JSONObject.fromObject(resultMap);
        return js.toString();

    }




    /*####################################################################################*/
    /*##############################   类别管理    #######################################*/
    /*####################################################################################*/
    /**
         * @Description 查询所有类别信息
         * @Author zhangxue
         * @Date 12:45 2018/5/31
         * @Param
         * @retrun
         **/
    @RequestMapping(value="/queryAllCategory",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String queryAllCategory(@RequestBody Map<String,Object> param,HttpSession session){
        /*Integer i = SessionAndGetRemote.SessionQuery(session);
        if(i==-1){
            Map<String,Object> tMap = new HashMap<String,Object>();
            tMap.put("status","1");
            JSONObject js = JSONObject.fromObject(tMap);
            return js.toString();
        }*/
        String url = BACKURL+"/queryAllCategory";
        Map<String,Object> statusMap = SessionAndGetRemote.getRemoteResult(param,url,okHttpUtils);
        String data = statusMap.get("data")!=null?statusMap.get("data").toString():null;
        return data;
    }

    /**
         * @Description 新增类别信息
         * @Author zhangxue
         * @Date 14:45 2018/6/1
         * @Param
         * @retrun
         **/
    @RequestMapping(value="/saveCategory",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String saveCategory(@RequestBody Map<String,Object> param,HttpSession session){
        String username = session.getAttribute("username")!=null?session.getAttribute("username").toString():null;
        if(username==null){
            return null;
        }
        param.put("InputMan",username);
        String url = BACKURL+"/saveCategory";
        Map<String,Object> statusMap = SessionAndGetRemote.getRemoteResult(param,url,okHttpUtils);
        String data = statusMap.get("data")!=null?statusMap.get("data").toString():null;
        return data;
    }

    /**
         * @Description 删除类别信息
         * @Author zhangxue
         * @Date 14:46 2018/6/1
         * @Param
         * @retrun
         **/
    @RequestMapping(value="/deleteCategoryById",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String deleteCategoryById(@RequestParam Map<String,Object> param,HttpSession session){
        String url = BACKURL+"/deleteCategoryById";
        Map<String,Object> statusMap = SessionAndGetRemote.getRemoteResult(param,url,okHttpUtils);
        String data = statusMap.get("data")!=null?statusMap.get("data").toString():null;
        return data;
    }

    /**
     * @Description 修改类别信息
     * @Author zhangxue
     * @Date 14:46 2018/6/1
     * @Param
     * @retrun
     **/
    @RequestMapping(value="/updateCategoryById",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String updateCategoryById(@RequestBody Map<String,Object> param,HttpSession session){
        String username = session.getAttribute("username")!=null?session.getAttribute("username").toString():null;
        if(username==null){
            return null;
        }
        param.put("UpdateMan",username);
        String url = BACKURL+"/updateCategoryById";
        Map<String,Object> statusMap = SessionAndGetRemote.getRemoteResult(param,url,okHttpUtils);
        String data = statusMap.get("data")!=null?statusMap.get("data").toString():null;
        return data;
    }




    /*####################################################################################*/
    /*##############################   项目管理    #######################################*/
    /*####################################################################################*/

    /**
     * @Description 查询所有项目信息
     * @Author zhangxue
     * @Date 12:45 2018/5/31
     * @Param
     * @retrun
     **/
    @RequestMapping(value="/queryAllProject",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String queryAllProject(@RequestBody Map<String,Object> param,HttpSession session){
        String url = BACKURL+"/queryAllProject";
        Map<String,Object> statusMap = SessionAndGetRemote.getRemoteResult(param,url,okHttpUtils);
        String data = statusMap.get("data")!=null?statusMap.get("data").toString():null;
        return data;

    }

    /**
     * @Description 新增项目信息
     * @Author zhangxue
     * @Date 14:45 2018/6/1
     * @Param
     * @retrun
     **/
    @RequestMapping(value="/saveProject",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String saveProject(@RequestBody Map<String,Object> param,HttpSession session){
        String username = session.getAttribute("username")!=null?session.getAttribute("username").toString():null;
        if(username==null){
            return null;
        }
        param.put("InputMan",username);
        String url = BACKURL+"/saveProject";
        Map<String,Object> statusMap = SessionAndGetRemote.getRemoteResult(param,url,okHttpUtils);
        String data = statusMap.get("data")!=null?statusMap.get("data").toString():null;
        return data;
    }

    /**
     * @Description 修改项目信息
     * @Author zhangxue
     * @Date 14:46 2018/6/1
     * @Param
     * @retrun
     **/
    @RequestMapping(value="/updateProjectById",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String updateProjectById(@RequestBody Map<String,Object> param,HttpSession session){
        String username = session.getAttribute("username")!=null?session.getAttribute("username").toString():null;
        if(username==null){
            return null;
        }
        param.put("UpdateMan",username);
        String url = BACKURL+"/updateProjectById";
        Map<String,Object> statusMap = SessionAndGetRemote.getRemoteResult(param,url,okHttpUtils);
        String data = statusMap.get("data")!=null?statusMap.get("data").toString():null;
        return data;
    }




    /*####################################################################################*/
    /*##############################   岗位标准管理    #######################################*/
    /*####################################################################################*/

    /**
     * @Description 查询所有岗位标准信息
     * @Author zhangxue
     * @Date 12:45 2018/5/31
     * @Param
     * @retrun
     **/
    @RequestMapping(value="/queryAllStandard",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String queryAllStandard(@RequestBody Map<String,Object> param,HttpSession session){
        String url = BACKURL+"/queryAllStandard";
        Map<String,Object> statusMap = SessionAndGetRemote.getRemoteResult(param,url,okHttpUtils);
        String data = statusMap.get("data")!=null?statusMap.get("data").toString():null;
        return data;

    }
    /**
     * @Description 新增岗位标准信息
     * @Author zhangxue
     * @Date 14:45 2018/6/1
     * @Param
     * @retrun
     **/
    @RequestMapping(value="/saveStandard",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String saveStandard(@RequestBody Map<String,Object> param,HttpSession session){
        String username = session.getAttribute("username")!=null?session.getAttribute("username").toString():null;
        if(username==null){
            return null;
        }
        param.put("InputMan",username);
        String url = BACKURL+"/saveStandard";
        Map<String,Object> statusMap = SessionAndGetRemote.getRemoteResult(param,url,okHttpUtils);
        String data = statusMap.get("data")!=null?statusMap.get("data").toString():null;
        return data;
    }


    /**
     * @Description 删除岗位标准信息
     * @Author zhangxue
     * @Date 14:45 2018/6/1
     * @Param
     * @retrun
     **/
    @RequestMapping(value="/deleteStandardById",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String deleteStandardById(@RequestBody Map<String,Object> param,HttpSession session){
        String url = BACKURL+"/deleteStandardById";
        Map<String,Object> statusMap = SessionAndGetRemote.getRemoteResult(param,url,okHttpUtils);
        String data = statusMap.get("data")!=null?statusMap.get("data").toString():null;
        return data;
    }

    /**
     * @Description 修改岗位标准信息
     * @Author zhangxue
     * @Date 14:46 2018/6/1
     * @Param
     * @retrun
     **/
    @RequestMapping(value="/updateStandardById",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String updateStandardById(@RequestBody Map<String,Object> param,HttpSession session){
        String username = session.getAttribute("username")!=null?session.getAttribute("username").toString():null;
        if(username==null){
            return null;
        }
        param.put("UpdateMan",username);
        String url = BACKURL+"/updateStandardById";
        Map<String,Object> statusMap = SessionAndGetRemote.getRemoteResult(param,url,okHttpUtils);
        String data = statusMap.get("data")!=null?statusMap.get("data").toString():null;
        return data;
    }

    /*####################################################################################*/
    /*##############################   考核管理    #######################################*/
    /*####################################################################################*/

    /**
         * @Description 查询考核信息
         * @Author zhangxue
         * @Date 14:45 2018/6/3
         * @Param
         * @retrun
         **/
    @RequestMapping(value="/queryAllAssess",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String queryAllAssess(@RequestBody Map<String,Object> param){
        String url = BACKURL+"/queryAllAssess";
        Map<String,Object> statusMap = SessionAndGetRemote.getRemoteResult(param,url,okHttpUtils);
        String data = statusMap.get("data")!=null?statusMap.get("data").toString():null;
        return data;

    }

    /**
     * @Description 新增考核信息
     * @Author zhangxue
     * @Date 14:45 2018/6/1
     * @Param
     * @retrun
     **/
    @RequestMapping(value="/saveAssess",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String saveAssess(@RequestBody Map<String,Object> param,HttpSession session){
        String username = session.getAttribute("username")!=null?session.getAttribute("username").toString():null;
        if(username==null){
            return null;
        }
        param.put("InputMan",username);
        String url = BACKURL+"/saveAssess";
        Map<String,Object> statusMap = SessionAndGetRemote.getRemoteResult(param,url,okHttpUtils);
        String data = statusMap.get("data")!=null?statusMap.get("data").toString():null;
        return data;
    }

    /**
     * @Description 删除考核信息
     * @Author zhangxue
     * @Date 14:46 2018/6/1
     * @Param
     * @retrun
     **/
    @RequestMapping(value="/deleteAssessById",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String deleteAssessById(@RequestBody Map<String,Object> param){
        String url = BACKURL+"/deleteAssessById";
        Map<String,Object> statusMap = SessionAndGetRemote.getRemoteResult(param,url,okHttpUtils);
        String data = statusMap.get("data")!=null?statusMap.get("data").toString():null;
        return data;
    }

    /**
     * @Description 修改考核信息
     * @Author zhangxue
     * @Date 14:46 2018/6/1
     * @Param
     * @retrun
     **/
    @RequestMapping(value="/updateAssessById",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String updateAssessById(@RequestBody Map<String,Object> param,HttpSession session){
        String username = session.getAttribute("username")!=null?session.getAttribute("username").toString():null;
        if(username==null){
            return null;
        }
        param.put("UpdateMan",username);
        String url = BACKURL+"/updateAssessById";
        Map<String,Object> statusMap = SessionAndGetRemote.getRemoteResult(param,url,okHttpUtils);
        String data = statusMap.get("data")!=null?statusMap.get("data").toString():null;
        return data;
    }



    /**
     * @Description 考核录入---类别查询
     * @Author zhangxue
     * @Date 15:49 2018/6/5
     * @Param
     * @retrun
     **/
    @RequestMapping(value="/queryAccessCategory",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String queryAccessCategory(@RequestBody Map<String,Object> param){
        JSONObject jsonObject = JSONObject.fromObject(param);
        Map<String,String> headersParams = new HashMap<String,String>();
        headersParams.put("Content-Type","application/json;UTF-8");
        Map<String,Object> resultMap = okHttpUtils.sendPostJson(BACKURL+"/queryAccessCategory",headersParams,jsonObject.toString(),new Object());
        JSONObject js = new JSONObject();
        js = JSONObject.fromObject(resultMap);
        return js.toString();

    }

    /**
         * @Description 考核录入---项目查询
         * @Author zhangxue
         * @Date 17:48 2018/6/5
         * @Param
         * @retrun
         **/
    @RequestMapping(value="/queryAccessProject",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String queryAccessProject(@RequestBody Map<String,Object> param){
        JSONObject jsonObject = JSONObject.fromObject(param);
        Map<String,String> headersParams = new HashMap<String,String>();
        headersParams.put("Content-Type","application/json;UTF-8");
        Map<String,Object> resultMap = okHttpUtils.sendPostJson(BACKURL+"/queryAccessProject",headersParams,jsonObject.toString(),new Object());
        JSONObject js = new JSONObject();
        js = JSONObject.fromObject(resultMap);
        return js.toString();

    }

    /**
     * @Description 考核录入---岗位标准查询
     * @Author zhangxue
     * @Date 17:48 2018/6/5
     * @Param
     * @retrun
     **/
    @RequestMapping(value="/queryAccessStandard",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String queryAccessStandard(@RequestBody Map<String,Object> param){
        JSONObject jsonObject = JSONObject.fromObject(param);
        Map<String,String> headersParams = new HashMap<String,String>();
        headersParams.put("Content-Type","application/json;UTF-8");
        Map<String,Object> resultMap = okHttpUtils.sendPostJson(BACKURL+"/queryAccessStandard",headersParams,jsonObject.toString(),new Object());
        JSONObject js = new JSONObject();
        js = JSONObject.fromObject(resultMap);
        return js.toString();

    }

    /**
         * @Description 导出excel
         * @Author zhangxue
         * @Date 14:53 2018/6/14
         * @Param
         * @retrun
         **/
    @RequestMapping(value="/CreateAssessExcel",method = RequestMethod.GET,produces = "application/json;charset=utf-8")
    public void CreateAssessExcel(HttpServletRequest request, HttpServletResponse response,ServletOutputStream out){
        Map<String,Object> param = new HashMap<String, Object>();
        String AssessStartDate = request.getParameter("AssessDateStart")!=null?request.getParameter("AssessDateStart").toString():"";
        String AssessEndDate = request.getParameter("AssessDateEnd")!=null?request.getParameter("AssessDateEnd").toString():"";
        String Store = request.getParameter("Store")!=null?request.getParameter("Store").toString():"";
        String Post = request.getParameter("Post")!=null?request.getParameter("Post").toString():"";
        String SalerID = request.getParameter("SalerID")!=null?request.getParameter("SalerID").toString():"";
        String CID = request.getParameter("CID")!=null?request.getParameter("CID").toString():"";
        try {
            AssessStartDate = URLDecoder.decode(AssessStartDate, "UTF-8");
            AssessEndDate = URLDecoder.decode(AssessEndDate, "UTF-8");
            Store = URLDecoder.decode(Store, "UTF-8");
            Post = URLDecoder.decode(Post, "UTF-8");
            SalerID = URLDecoder.decode(SalerID, "UTF-8");
            CID = URLDecoder.decode(CID, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        param.put("AssessStartDate",AssessStartDate);
        param.put("AssessEndDate",AssessEndDate);
        param.put("Store",Store);
        param.put("Post",Post);
        param.put("SalerID",SalerID);
        param.put("CID",CID);
        JSONObject jsonObject = JSONObject.fromObject(param);
        Map<String,String> headersParams = new HashMap<String,String>();
        headersParams.put("Content-Type","application/json;UTF-8");
        System.out.println(jsonObject.toString());
        Map<String,Object> resultMap = okHttpUtils.sendPostJson(BACKURL+"/CreateAssessExcel",headersParams,jsonObject.toString(),new Object());
        String tempStr = resultMap.get("data").toString();
        JSONObject js = new JSONObject();
        js = JSONObject.fromObject(tempStr);
        JSONArray ja = JSONArray.fromObject(js.get("data"));
        List list = new ArrayList();
        for(int i=0;i<ja.size();i++){
            AssessExcel assessExcel = new AssessExcel();
            assessExcel.setBase(jxJson("Base",ja.get(i).toString()));
            assessExcel.setStore(jxJson("Store",ja.get(i).toString()));
            assessExcel.setSalerID(jxJson("SalerName",ja.get(i).toString()));
            assessExcel.setPost(jxJson("Post",ja.get(i).toString()));
            assessExcel.setCID(jxJson("CName",ja.get(i).toString()));
            assessExcel.setPID(jxJson("PName",ja.get(i).toString()));
            assessExcel.setSID(jxJson("SName",ja.get(i).toString()));
            assessExcel.setProportion(jxJson("Proportion",ja.get(i).toString()));
            assessExcel.setMoney(jxJson("Money",ja.get(i).toString()));
            assessExcel.setFinishRate(jxJson("FinishRate",ja.get(i).toString()));
            assessExcel.setAssessDate(jxJson("AssessDate",ja.get(i).toString()));
            list.add(assessExcel);
        }
        // 第一步，创建一个webbook，对应一个Excel文件
        HSSFWorkbook wb = new HSSFWorkbook();
        // 第二步，在webbook中添加一个sheet,对应Excel文件中的sheet
        HSSFSheet sheet = wb.createSheet("销售部考核记录");
        // 第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
        HSSFRow row = sheet.createRow((int) 0);
        // 第四步，创建单元格，并设置值表头 设置表头居中
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式

        HSSFCell cell = row.createCell((short) 0);
        cell.setCellValue("中心店面");
        cell.setCellStyle(style);
        cell = row.createCell((short) 1);
        cell.setCellValue("销售人员姓名");
        cell.setCellStyle(style);
        cell = row.createCell((short) 2);
        cell.setCellValue("岗位");
        cell.setCellStyle(style);
        cell = row.createCell((short) 3);
        cell.setCellValue("类别");
        cell.setCellStyle(style);
        cell = row.createCell((short) 4);
        cell.setCellValue("项目");
        cell.setCellStyle(style);
        cell = row.createCell((short) 5);
        cell.setCellValue("岗位标准");
        cell.setCellStyle(style);
        cell = row.createCell((short) 6);
        cell.setCellValue("基数");
        cell.setCellStyle(style);
        cell = row.createCell((short) 7);
        cell.setCellValue("占比");
        cell.setCellStyle(style);
        cell = row.createCell((short) 8);
        cell.setCellValue("金额");
        cell.setCellStyle(style);
        cell = row.createCell((short) 9);
        cell.setCellValue("完成率");
        cell.setCellStyle(style);
        cell = row.createCell((short) 10);
        cell.setCellValue("考核时间");
        cell.setCellStyle(style);


        // 第五步，写入实体数据 实际应用中这些数据从数据库得到，

        for (int i = 0; i < list.size(); i++)
        {
            row = sheet.createRow((int) i + 1);
            AssessExcel assessExcel = (AssessExcel) list.get(i);
            // 第四步，创建单元格，并设置值
            row.createCell((short) 0).setCellValue(assessExcel.getStore());
            row.createCell((short) 1).setCellValue(assessExcel.getSalerID());
            row.createCell((short) 2).setCellValue(assessExcel.getPost());
            row.createCell((short) 3).setCellValue(assessExcel.getCID());
            row.createCell((short) 4).setCellValue(assessExcel.getPID());
            row.createCell((short) 5).setCellValue(assessExcel.getSID());
            row.createCell((short) 6).setCellValue(assessExcel.getBase());
            row.createCell((short) 7).setCellValue(assessExcel.getProportion());
            row.createCell((short) 8).setCellValue(assessExcel.getMoney());
            row.createCell((short) 9).setCellValue(assessExcel.getFinishRate());
            cell = row.createCell((short) 10);
            String assDateStr = formatData("yyyy-MM-dd",Long.valueOf(assessExcel.getAssessDate()));
            cell.setCellValue(assDateStr);
        }
        // 第六步，将文件存到指定位置
        try
        {
//            response.setHeader(
//                    "Content-disposition",
//                    "attachment; filename="
//                            + new String(
//                            ("D:/AssessExcel/AssessExcel.xls").getBytes("GB2312"),
//                            "ISO8859-1"));
            // 设定输出文件头
            response.setContentType("application/vnd.ms-excel");
            wb.write(out);
            out.flush();
            out.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }


    public String jxJson(String mkey, String strJson) {
        String value = "";
        try {
            JSONObject json = JSONObject.fromObject(strJson);
            Iterator iterator = json.keys();
            while (iterator.hasNext()) {
                String key = iterator.next() + "";

                if (json.getString(key).startsWith("{")) {
                    value = jxJson(mkey, json.getString(key));
                    break;
                } else {
                    if (key.equals(mkey)) {
                        value = json.getString(key);
                        break;
                    }
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        if (value.equals("null")) {
            value = "";
        }
        return value;
    }

    /**
     * 时间戳转换成日期格式字符串
     * @param
     * @param
     * @return
     */
    public static String formatData(String dataFormat, long timeStamp) {
        if (timeStamp == 0) {
            return "";
        }
        SimpleDateFormat format = new SimpleDateFormat(dataFormat);
        return format.format(new Date(timeStamp));
    }

}
